package com.dianbo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.TotalClass;
import util.UtilDAO;

import com.dianbo.bean.DianboType;
import com.jifen.bean.JiFen;
import com.jifen.dao.JifenDao;

public class DianboTypeDao {

/**
 * 
 *@author qingyu zhang
 *@function:根据条件查询点播类型
 * @param table
 * @param condition
 * @param order
 * @return
 *2011-4-5
 *上午07:54:10
 *zzClub
 *com.dianbo.dao
 *List<DianboType>
 */
	// 要执行的语句
	private String sql = "";
	private DianboType dianboType=null;
	private List<DianboType> dianboTypeList = null;
	private ResultSet rs = null;
	
	/**
	 * 
	 *@author qingyu zhang
	 *@function:add a dianboType
	 * @param dianboType
	 * @return
	 *2011-4-5
	 *下午03:55:43
	 *zzClub
	 *com.dianbo.dao
	 *boolean
	 */
	public boolean addDianboType(DianboType dianboType){
		boolean bo = false;
		sql="insert into tb_qsType_answer (typeId,userId,typeName,createTime) " +
				"values('"+dianboType.getTypeId()+"',"+dianboType.getUserId()+",'"+dianboType.getTypeName()+"','"+dianboType.getCreateTime()+"')";
		bo = new TotalClass().operatorObject(sql);
		return bo ;
	}
	
	/**
	 * 
	 *@author qingyu zhang
	 *@function:modify a DianboType
	 * @param dianboType
	 * @return
	 *2011-4-5
	 *下午04:05:11
	 *zzClub
	 *com.dianbo.dao
	 *boolean
	 */
	public boolean modifyDianboType(DianboType dianboType){
		boolean bo = false;
		sql="update tb_qsType_answer set typeName='"+dianboType.getTypeName()+"',createTime='"+dianboType.getCreateTime()+"' where typeId='"+dianboType.getTypeId()+"'";
		System.out.println("修改系列"+sql);
		bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	public List<DianboType> getDianboList(String table,String condition,String order){
		sql= UtilDAO.getList(table, order, condition);
		System.out.println("sql语句＝"+sql);
		ResultSet rs =  new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				DianboType dianbo = getDianboType(rs);
				if(dianboTypeList==null){
					dianboTypeList = new ArrayList<DianboType>();
				}
				dianboTypeList.add(dianbo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dianboTypeList;
	}
	
	public DianboType  getDianboType(ResultSet rs){
		DianboType dbType=new DianboType();
		try {
			dbType.setCreateTime(rs.getString("createTime"));
			dbType.setTypeId(rs.getString("typeId"));
			dbType.setTypeName(rs.getString("typeName"));
			dbType.setUserId(rs.getInt("userId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dbType;
	}
	
	/*public static Object getObj(ResultSet rs){
		Object obj = new Object();
		try {
			ResultSetMetaData rsd= rs.getMetaData();
			int colNum = rsd.getColumnCount();//得到表中的列数
			if(colNum>0){
				for(int i = 1;i<colNum;i++){
					String colName = rsd.getColumnName(i);
					int colType = rsd.getColumnType(i);
					String colTypeName = "";
					if(colType==12){//String
						colTypeName="String";
						rs.getString(colName);
					}else if(colType==4){//int
						colTypeName="int";
						rs.getInt(colName);
					}else if(colType==2004){//blob
						rs.getBlob(colName);
					}else if(colType==16){//boolean
						rs.getBoolean(colName);
					}else if(colType==2005){//clob
						rs.getClob(colName);
					}else if(colType==91){//date
						rs.getDate(colName);
					}else if(colType==8){//double
						rs.getDouble(colName);
					}else if(colType==6){//float
						rs.getFloat(colName);
					}else if(colType==93){//TIMESTAMP
						rs.getTimestamp(colName);
					}else if(colType==92){//time
						rs.getTime(colName);
					}
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}*/
	/**
	 * 验证此名称是否存在
	 */
	public boolean getTypeByName(String typeName){
		boolean bo = false;
		sql="select * from tb_qsType_answer where typeName ='"+typeName+"'";
		System.out.println("sql==="+sql);
		rs = new TotalClass().getResult(sql);
		try {
			if(rs.next()){
				bo = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bo;
	}
	public String getMaxtypeid(){
		String maxTypeid="";
		sql="select top 1 * from  tb_qsType_answer order by createTime desc";
		rs=new TotalClass().getResult(sql);
		try{
			if(rs.next()){
				maxTypeid=rs.getString(0);
				System.out.println("maxTypeid:"+maxTypeid);
			}
		}catch(SQLException ex){
			ex.printStackTrace();
		}
		return maxTypeid;
	}
	
	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据主键ID得到单个对象
	 * @param typeId
	 * @return
	 *2011-4-11
	 *下午08:43:27
	 *zzClub
	 *com.dianbo.dao
	 *DianboType
	 */
	public DianboType getDianboType(String typeId){
		DianboType dianboType = null;
		sql = "select * from tb_qsType_answer where typeId='"+typeId+"'";
		System.out.println("sql==="+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(dianboType==null){
					dianboType = new DianboType();
				}
				dianboType = this.getDianboType(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dianboType;
	}
	public DianboType getDianboTypebyName(String typeName){
		DianboType dianboType = null;
		sql = "select * from tb_qsType_answer where typeName='"+typeName+"'";
		System.out.println("sql==="+sql);
		rs = new TotalClass().getResult(sql);
		try {
			while(rs.next()){
				if(dianboType==null){
					dianboType = new DianboType();
				}
				dianboType = this.getDianboType(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dianboType;
	}
	public static void main(String args[]){
		List<DianboType> dianbaoList =new DianboTypeDao().getDianboList("tb_qsType_answer","", "createTIme");
		for(DianboType dbtype:dianbaoList){
			System.out.println("name=="+dbtype.getTypeName());
		}
	}
}
